package com.lzw.bluetoothpractice.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzw.bluetoothpractice.Activitys.MyDoctorsActivity;
import com.lzw.bluetoothpractice.Adapters.RecoveryActionListAdapter;
import com.lzw.bluetoothpractice.Adapters.UserReadMessageListAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.PlanBean;
import com.lzw.bluetoothpractice.bean.SystemUserListBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.Map;

import static com.lzw.bluetoothpractice.MyApplication.getContext;

public class TodayTargetFragment extends Fragment {
    private Map<String, String> keyMap;
    Context mcontext;
    RecyclerView rvTodayActionList;
    RecoveryActionListAdapter recoveryActionListAdapter;

    public TodayTargetFragment(Context context) {
        mcontext=context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.today_target,container,false);
        rvTodayActionList=v.findViewById(R.id.rv_planList);
        InitView();
        initMyDoctor();
        return v;
    }
    private  void InitView(){



    }

    private void initMyDoctor(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/doctor/list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
        String userid = SharePreferenceUtils.getString(getContext(),"userId",null);
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getPlan(Long.parseLong("1"),"1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("计划1：",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                PlanBean planBean = JSON.parseObject(json.toString(), PlanBean.class);
                if(planBean.getCode()==200){

                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager2=new LinearLayoutManager(getContext());
                    linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
                    rvTodayActionList.setLayoutManager(linearLayoutManager2);
                    //   rvTodayActionList.addItemDecoration(new DividerItemDecoration(mcontext,DividerItemDecoration.VERTICAL));
                    recoveryActionListAdapter=new RecoveryActionListAdapter(mcontext);
                    recoveryActionListAdapter.updateData(planBean.getData());
                    rvTodayActionList.setAdapter(recoveryActionListAdapter);



                }else {
                    Toast toast=Toast.makeText(getContext(),"查询我的医生失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }
}
